global.globalKey="123456"
const  jwtUtil=require('../utils/jwtUtils')
module.exports=class users_dao extends  require('../model/users_mod'){

   static  async Login(req,resp){
      // let body =req.body
      // let loginData = await this.LoginUser(body.username,body.password,body.type)
      // resp.send(loginData)
      let body= req.body
      let loginData= await  this.LoginUser(body.username,body.password,body.type)
      let no=0
      // let loginData= await  this.LoginUserByid(body.username,body.password,body.type)
      //如果获取到了登录用户信息则登陆成功
      if (loginData.length!=0){
         //生成token
         let jwt_token= jwtUtil.sign({
            id:loginData[0].id,
            username:loginData[0].username,
            head:loginData[0].head,
            type:loginData[0].type,
            classes:loginData[0].classes,
            address:loginData[0].address,
            createtime:loginData[0].createtime,
            sex:loginData[0].sex
         },global.globalKey,3600)
         // resp.send({loginData,jwt_token})
         resp.send({no,jwt_token})
      }else  resp.status(500).send("用户名或者密码输入错误")
   }
   /**
    * 根据token解析成用户信息
    * @param req
    * @param resp
    */
   static async getUserDataByToken(req,resp){
      let token = String(req.headers.authorization || '').split(' ').pop();
      let result=await jwtUtil.verifysync(token,global.globalKey)
      resp.send(result)
   }
   /**
    * 根据用户类型进行用户信息获取(分页获取总数量与数据)
    * @param req
    * @param resp
    */
   static async getUsersByTypePage(req,resp){
      console.log(req.query)
      let query=req.query;
      let data = await this.getUsersByTypePageMod(query.type,query.currPage,query.pageNum)
      let total=await this.getUsersByTypePageTotal(query.type)
      resp.send({data,total:total[0].count})
   }
   /**
    * 用户删除(同时清空该用户阅读记录
    * @param req
    * @param resp
    */
   static async delUserdata(req,resp){
      let results= await this.delUserdataMod(req.query.u_id)
      results+=await  this.delRead(req.query.u_id)
      resp.send(results)
   }
   /**
    * 更改用户信息
    * @param req
    * @param resp
    */
   static async upUserdata(req,resp){
      let body= req.body
      let u_id=body.params.u_id
      let username=body.params.username
      let sex=body.params.sex
      let address=body.params.address
      let type=body.params.type

      console.log(body.params.u_id)
      console.log(body)
      let results= await this.upUserdataMod(u_id,username,sex,address,type)
      resp.send(results)
   }
   /**
    * **************************************修改个人信息**************************************
    */
   /**
    * 修改个人密码
    * @param req
    * @param resp
    * @returns {Promise<void>}
    */
   static  async upPwd(req,resp){
      console.log('xxx',req.body)
      let body = req.body.params
      let token = String(req.headers.authorization || '').split(' ').pop();
      let verify=await  jwtUtil.verifysync(token,globalKey)
      let u_id=verify.id
      let oldpassword=body.oldpassword
      let newpassword=body.newpassword
      let results=await this.upPwdMod(u_id,oldpassword,newpassword)
      if (results.changedRows==0)        resp.send("修改失败,可能是旧密码错误或者新密码与旧密码相同")
      else  resp.send("密码修改成功")
   }
   /**
    * 将redis的xlsx数据写入数据库
    * @param req
    * @param resp
    * @returns {Promise<void>}
    */
   static async setXlsxData(req,resp){
      // console.log(req)
      let xlsxData=req
      xlsxData= await xlsxData[0].data
      let AllUsers=await this.getAllUserX();
      let inXlsxArr=[];
      for (let i=1;i<xlsxData.length;i++){
         if (xlsxData[i][0]!=0){
            let xlsxObj={};
            let falg=true;
            xlsxObj.id=xlsxData[i][0]
            xlsxObj.username=xlsxData[i][1]
            xlsxObj.password=xlsxData[i][2]
            xlsxObj.head=xlsxData[i][3]||'1.jpg'
            xlsxObj.address=xlsxData[i][4]
            xlsxObj.sex=xlsxData[i][5]
            xlsxObj.classes=xlsxData[i][6]
            xlsxObj.type=xlsxData[i][7]
            for (let j=0;j<AllUsers.length;j++){
               if (xlsxObj.id==AllUsers[j].id) falg=false
            }
            if (falg) inXlsxArr.push(xlsxObj)
         }
      }

         if (inXlsxArr.length!=0) {
            this.inXlsxData(inXlsxArr)
            resp.send("导入数据成功")
            console.log('点击导入',inXlsxArr)
         }
         else{
            resp.status(500).send("导入文件中的数据已重复,导入失败")
         }

   }
   /**
    * 获取所有今日超过37.5高危人群
    */
   static async getHotTmp(req, resp) {
      let pageNum = req.query.pageNum;
      let currPage = req.query.currPage;
      let newDate = this.getNowAndLastDate().newDate
      let lastDate = this.getNowAndLastDate().lastDate
      // let data = await this.getHotTmpMod( newDate, lastDate)
      let data = await this.getHotTmpMod(pageNum, currPage,newDate, lastDate)
      let total = await this.getHotTmpTotal(newDate, lastDate)
      resp.send({data, total: total[0].count})
   }


}